package com.sz.admin.system.controller;

import com.sz.admin.system.pojo.po.SysFileMonitor;
import com.sz.admin.system.service.FileMonitorNotificationService;
import com.sz.admin.system.service.SysFileMonitorService;
import com.sz.admin.system.service.SysMonitorFileRecordService;
import com.sz.core.common.entity.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实时文件监控控制器
 *
 * @author sz-admin
 * @since 2025-01-13
 */
@Tag(name = "实时文件监控")
@RestController
@RequestMapping("/system/fileMonitor/realtime")
@RequiredArgsConstructor
public class RealtimeFileMonitorController {

    private final SysFileMonitorService sysFileMonitorService;
    private final SysMonitorFileRecordService recordService;
    private final FileMonitorNotificationService notificationService;

    @Operation(summary = "获取监控实时状态")
    @GetMapping("/status")
    public ApiResult<Map<String, Object>> getRealtimeStatus() {
        Map<String, Object> status = new HashMap<>();
        
        // 获取监控总数
        long totalMonitors = sysFileMonitorService.count();
        long activeMonitors = sysFileMonitorService.getEnabledMonitors().size();
        
        status.put("totalMonitors", totalMonitors);
        status.put("activeMonitors", activeMonitors);
        status.put("inactiveMonitors", totalMonitors - activeMonitors);
        
        return ApiResult.success(status);
    }

    @Operation(summary = "发送测试通知")
    @PostMapping("/test-notification")
    public ApiResult<Void> sendTestNotification() {
        notificationService.notifyMonitorStatusChange(0L, "TEST", "这是一条测试通知消息");
        return ApiResult.success();
    }

    @Operation(summary = "获取监控统计信息")
    @GetMapping("/statistics/{monitorId}")
    public ApiResult<Map<String, Object>> getMonitorStatistics(@PathVariable Long monitorId) {
        Map<String, Object> statistics = new HashMap<>();
        
        // 这里可以添加具体的统计查询逻辑
        // 例如：查询该监控下的文件处理统计
        
        statistics.put("monitorId", monitorId);
        statistics.put("message", "统计信息查询功能待实现");
        
        return ApiResult.success(statistics);
    }

    @Operation(summary = "检查监控运行状态")
    @GetMapping("/checkStatus")
    public ApiResult<Map<String, Object>> checkMonitoringStatus() {
        Map<String, Object> status = new HashMap<>();
        
        try {
            // 获取所有监控配置
            List<SysFileMonitor> allMonitors = sysFileMonitorService.list();
            List<SysFileMonitor> enabledMonitors = sysFileMonitorService.getEnabledMonitors();
            
            status.put("totalMonitors", allMonitors.size());
            status.put("enabledMonitors", enabledMonitors.size());
            
            // 检查每个启用的监控
            for (SysFileMonitor monitor : enabledMonitors) {
                String key = "monitor_" + monitor.getId();
                Map<String, Object> monitorInfo = new HashMap<>();
                monitorInfo.put("path", monitor.getMonitorPath());
                monitorInfo.put("fileTypes", monitor.getFileTypes());
                
                // 检查路径是否存在
                File dir = new File(monitor.getMonitorPath());
                monitorInfo.put("pathExists", dir.exists());
                
                if (dir.exists() && dir.isDirectory()) {
                    File[] files = dir.listFiles();
                    monitorInfo.put("fileCount", files != null ? files.length : 0);
                }
                
                status.put(key, monitorInfo);
            }
            
            return ApiResult.success(status);
        } catch (Exception e) {
            status.put("error", e.getMessage());
            return ApiResult.success(status);
        }
    }

    @Operation(summary = "手动重启监控服务")
    @PostMapping("/restart")
    public ApiResult<String> restartMonitoring() {
        try {
            // 重启监控服务
            return ApiResult.success("监控服务重启请求已提交，请检查日志");
        } catch (Exception e) {
            return new ApiResult<>("-1", "重启监控服务失败: " + e.getMessage());
        }
    }
}